package com.example.jspwork.Servlet;

import com.example.jspwork.Dao.AddressQuery;
import com.example.jspwork.pojo.ResponseWrapper;
import com.example.jspwork.pojo.ShowIndex;

import com.example.jspwork.utils.MyUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;



@WebServlet("/api/index")
public class IndexServlet  extends HttpServlet {


    private List<ShowIndex> showIndexList =  initIndex();

    private List<ShowIndex> initIndex(){
        List<ShowIndex> list = new ArrayList<>();
        list.add(new ShowIndex(1, "北京", "巴中", "四川省", "中国", "11", 1, 1, 1, "翻倍", "是", MyUtil.DTStr(LocalDateTime.now())));
        return list;
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        req.setCharacterEncoding("UTF-8");
        String  str   =req.getParameter("params");
        System.out.println(str);
        showIndexList =queryDress(str);
        String jsonData=getJsonData();
        resp.setContentType("application/json");
        resp.setCharacterEncoding("UTF-8");
        resp.getWriter().write(jsonData);

    }

    private List<ShowIndex> queryDress(String address) {

        List<ShowIndex> list;
        list = AddressQuery.queryDress(address);

        return list;
    }
    public String getJsonData() {
//        List<nowBean> infoList = nowinfo_list();
        ObjectMapper mapper = new ObjectMapper();
        try {
            ResponseWrapper responseWrapper = new ResponseWrapper(200, showIndexList);
            return mapper.writeValueAsString(showIndexList);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return null;
        }
    }
}
